﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using netDxf;
using netDxf.Blocks;
using netDxf.Collections;
using netDxf.Entities;
using netDxf.Header;
using netDxf.Objects;
using netDxf.Tables;
using netDxf.Units;
using Attribute = netDxf.Entities.Attribute;
using Image = netDxf.Entities.Image;
using Point = netDxf.Entities.Point;
using Trace = netDxf.Entities.Trace;
using System.Threading;

namespace TestDxfDocument
{
    class MillingCuttersWithArborHoleForIndexableInserts
    {
        public static void FaceMillingCutter(Dictionary<string, Object> data)//面铣刀
        {
            DxfDocument dxf = new DxfDocument();
            //图层
            Layer contour = new Layer("contour") { Color = AciColor.Cyan };    //轮廓层
            Layer dimensioning = new Layer("dimensioning");    //尺寸层
            Layer refin = new Layer("refin") { Color = AciColor.Red };    //基准线/内轮廓
            Layer centre = new Layer("centre") { Color = AciColor.Yellow };    //中性线
            Layer textlayer = new Layer("textlayer") { Color = AciColor.Blue };    //文本层
            Layer hatching = new Layer("hatching") { Color = AciColor.Blue };    //填充层
            Layer additionalline = new Layer("additionalline") { Color = AciColor.Cyan };    //附加线
            Layer cutting = new Layer("cutting") { Color = AciColor.Red };    //切削层
            Layer nocutting = new Layer("nocutting");    //非切削层
            Layer skcontour = new Layer("skcontour") { Color = AciColor.Cyan };    //sk轮廓层
            Layer skdimensioning = new Layer("skdimensioning");    //sk尺寸线层
            Layer skrefin = new Layer("skrefin") { Color = AciColor.Red };    //sk基准线/内轮廓
            Layer sktext = new Layer("sktext") { Color = AciColor.Blue };  //sk文本
            Layer skhatching = new Layer("skhatching") { Color = AciColor.Blue };    //sk填充
            Layer skadditionlline = new Layer("skadditionlline") { Color = AciColor.Cyan };    //sk附加线
            Layer skviewcontour = new Layer("skviewcontour") { Color = AciColor.Cyan };    //skview轮廓
            Layer skviewdimensioning = new Layer("skviewdimensioning");    //skview尺寸线层
            //作图
            //坐标
            double DCON = Convert.ToDouble(data["DCON"]);
            double DC = Convert.ToDouble(data["DC"]);
            double DHUB = Convert.ToDouble(data["DHUB"]);
            double DCCB = Convert.ToDouble(data["DCCB"]);
            double BD = Convert.ToDouble(data["BD"]);
            double LCCB = Convert.ToDouble(data["LCCB"]);
            double OAL = Convert.ToDouble(data["OAL"]);
            double KAPR = Convert.ToDouble(data["KAPR"]);
            double LH = Convert.ToDouble(data["LH"]);
            string path = Convert.ToString(data["path"]);
            Vector2 v1 = new Vector2(-DC / 2.00, 0);
            Vector2 v2 = new Vector2(-BD / 2.00, (BD - DC) / 2.00 * Math.Tan(KAPR * (Math.PI / 180)));
            Vector2 v3 = new Vector2(v2.X, LH - 0.5);
            Vector2 v4 = new Vector2(v3.X + 0.5, LH);
            Vector2 v5 = new Vector2(-DHUB / 2.00 - 0.5, LH);
            Vector2 v7 = new Vector2(-DHUB / 2.00, v5.Y + 0.5);
            Vector2 v6 = new Vector2(v5.X, v7.Y);
            Vector2 v8 = new Vector2(v7.X, OAL - 0.5);
            Vector2 v10 = new Vector2(v8.X + 0.5, OAL);
            Vector2 v9 = new Vector2(v10.X, v8.Y);
            Vector2 v11 = new Vector2(-v10.X, v10.Y);
            Vector2 v12 = new Vector2(-v9.X, v9.Y);
            Vector2 v13 = new Vector2(-v8.X, v8.Y);
            Vector2 v14 = new Vector2(-v7.X, v7.Y);
            Vector2 v15 = new Vector2(-v6.X, v6.Y);
            Vector2 v16 = new Vector2(-v5.X, v5.Y);
            Vector2 v17 = new Vector2(-v4.X, v4.Y);
            Vector2 v18 = new Vector2(-v3.X, v3.Y);
            Vector2 v19 = new Vector2(-v2.X, v2.Y);
            Vector2 v20 = new Vector2(-v1.X, v1.Y);
            Vector2 v21 = new Vector2(-DCCB / 2.00, 0);
            Vector2 v22 = new Vector2(v21.X, LCCB);
            Vector2 v23 = new Vector2(-v22.X, v22.Y);
            Vector2 v24 = new Vector2(v23.X, 0);
            Vector2 v25 = new Vector2(-DCON / 2.00, LCCB);
            Vector2 v26 = new Vector2(v25.X, OAL);
            Vector2 v27 = new Vector2(-v26.X, v26.Y);
            Vector2 v28 = new Vector2(-v25.X, v25.Y);
            Vector2 v29 = new Vector2(0, OAL + 2);
            Vector2 v30 = new Vector2(0, -2);
            Vector2 v31 = new Vector2(v19.X, 0);
            //线段
            Line Line1 = new Line(v1, v2);
            Line Line2 = new Line(v2, v3);
            Line Line3 = new Line(v3, v4);
            Line Line4 = new Line(v4, v5);
            Arc Arc1 = new Arc(v6, 0.5, -90, 0);
            Line Line5 = new Line(v7, v8);
            Arc Arc2 = new Arc(v9, 0.5, 90, 180);
            Line Line6 = new Line(v10, v11);
            Arc Arc3 = new Arc(v12, 0.5, 0, 90);
            Line Line7 = new Line(v13, v14);
            Arc Arc4 = new Arc(v15, 0.5, -180, -90);
            Line Line8 = new Line(v16, v17);
            Line Line9 = new Line(v17, v18);
            Line Line10 = new Line(v18, v19);
            Line Line11 = new Line(v19, v20);
            Line Line12 = new Line(v20, v1);
            Line Line13 = new Line(v5, v16);
            Line Line14 = new Line(v3, v18);
            Line Line15 = new Line(v2, v19);
            Line Line16 = new Line(v21, v22);
            Line Line17 = new Line(v22, v23);
            Line Line18 = new Line(v23, v24);
            Line Line19 = new Line(v25, v26);
            Line Line20 = new Line(v27, v28);
            Line Line21 = new Line(v29, v30);
            Line1.Layer = contour;
            Line2.Layer = contour;
            Line3.Layer = contour;
            Line4.Layer = contour;
            Line5.Layer = contour;
            Line6.Layer = contour;
            Line7.Layer = contour;
            Line8.Layer = contour;
            Line9.Layer = contour;
            Line10.Layer = contour;
            Line11.Layer = contour;
            Line12.Layer = contour;
            Line13.Layer = contour;
            Line14.Layer = contour;
            Line15.Layer = contour;
            Arc1.Layer = contour;
            Arc2.Layer = contour;
            Arc3.Layer = contour;
            Arc4.Layer = contour;
            Line16.Layer = refin;
            Line17.Layer = refin;
            Line18.Layer = refin;
            Line19.Layer = refin;
            Line20.Layer = refin;
            Line21.Layer = centre;
            dxf.AddEntity(Line1);
            dxf.AddEntity(Line2);
            dxf.AddEntity(Line3);
            dxf.AddEntity(Line4);
            dxf.AddEntity(Line5);
            dxf.AddEntity(Line6);
            dxf.AddEntity(Line7);
            dxf.AddEntity(Line8);
            dxf.AddEntity(Line9);
            dxf.AddEntity(Line10);
            dxf.AddEntity(Line11);
            dxf.AddEntity(Line12);
            dxf.AddEntity(Line13);
            dxf.AddEntity(Line14);
            dxf.AddEntity(Line15);
            dxf.AddEntity(Line16);
            dxf.AddEntity(Line17);
            dxf.AddEntity(Line18);
            dxf.AddEntity(Line19);
            dxf.AddEntity(Line20);
            dxf.AddEntity(Line21);
            dxf.AddEntity(Arc1);
            dxf.AddEntity(Arc2);
            dxf.AddEntity(Arc3);
            dxf.AddEntity(Arc4);
            //标注样式
            //箭头样式
            DimensionStyle dimensionStyle = fun.CreateDimStyle();
            dimensionStyle.ArrowSize = 0.2;
            dimensionStyle.DimArrow1 = DimensionArrowhead.Closedfilled;
            dimensionStyle.DimArrow2 = DimensionArrowhead.Closedfilled;
            //文本样式
            dimensionStyle.TextHeight = 1.0 / 2;
            double offset = 10;
            //线段尺寸标注
            //尺寸1
            LinearDimension dim1 = new LinearDimension(new Line(v3, v18), offset + 10 + OAL - LH + 0.5, 0, dimensionStyle);
            dim1.UserText = "BD = " + Math.Round(BD, 2);
            dxf.AddEntity(dim1);
            //尺寸2
            LinearDimension dim2 = new LinearDimension(new Line(v8, v13), offset + 5.5, 0, dimensionStyle);
            dim2.UserText = "DHUB = " + Math.Round(DHUB, 2);
            dxf.AddEntity(dim2);
            //尺寸3
            LinearDimension dim3 = new LinearDimension(new Line(v26, v27), offset, 0, dimensionStyle);
            dim3.UserText = "DCON = " + Math.Round(DCON, 2);
            dxf.AddEntity(dim3);
            //尺寸4
            LinearDimension dim4 = new LinearDimension(new Line(v21, v24), -offset, 0, dimensionStyle);
            dim4.UserText = "DCCB = " + Math.Round(DCCB, 2);
            dxf.AddEntity(dim4);
            //尺寸5
            LinearDimension dim5 = new LinearDimension(new Line(v1, v20), -offset - 5, 0, dimensionStyle);
            dim5.UserText = "DC = " + Math.Round(DC, 2);
            dxf.AddEntity(dim5);
            //尺寸6
            LinearDimension dim6 = new LinearDimension(new Line(v21, v22), -offset - (BD - DCCB) / 2.00, 270, dimensionStyle);
            dim6.UserText = "LCCB = " + Math.Round(LCCB, 2);
            dxf.AddEntity(dim6);
            //尺寸7
            LinearDimension dim7 = new LinearDimension(new Line(v17, v20), offset + (v17.X - v20.X) / 2.00 + 5.5, 270, dimensionStyle);
            dim7.UserText = "LH = " + Math.Round(LH, 2);
            dxf.AddEntity(dim7);
            //尺寸8
            LinearDimension dim8 = new LinearDimension(new Line(v11, v20), offset + (v20.X - v11.X) / 2.00 + (v19.X - v20.X) + 10.5, 270, dimensionStyle);
            dim8.UserText = "OAL = " + Math.Round(OAL, 2);
            dxf.AddEntity(dim8);
            //尺寸9
            Angular2LineDimension dim9 = new Angular2LineDimension(new Line(v20, v19), new Line(v20, v31), offset, dimensionStyle);
            dim9.UserText = String.Format("KAPR={0}°", KAPR);
            dxf.AddEntity(dim9);
            //定义格式，根据路径导出
            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;
            dxf.Save(path);
        }
    }
}
